home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 August / August CD.bin / Shareware / Education / numericalmethods Folder / chap_10 / crnich.m < prev    next >
Encoding:
Text File  |  1994-06-05  |  1018 b   |  43 lines  |  [MATF/MATL]

  1. function U = crnich(f,g1,g2,a,b,c,n,m)
  2. % U = crnich(f,g1,g2,a,b,c,n,m)
  3. % Crank-Nicholson solution to the heat equation.
  4. % This subroutine uses trisys.m to solve a tri-diagonal system.
  5. % f  is a function, input.
  6. % g1 is a function, input.
  7. % g2 is a function, input.
  8. % a  is the width of [0 a], input.
  9. % b  is the width of [0 b], input.
  10. % c  is the constant in the heat equation, input.
  11. % n  is the number of grid points over [0 a], input.
  12. % m  is the number of grid points over [0 b], input.
  13. % U  is the solution matrix, output.
  14. h = a/(n-1);
  15. k = b/(m-1);
  16. r = c^2*k/h^2;
  17. s1 = 2 + 2/r;
  18. s2 = 2/r - 2;
  19. U = zeros(n,m);
  20. for j=1:m,
  21.      U(1,j) = feval(g1,k*(j-1));
  22.      U(n,j) = feval(g2,k*(j-1));
  23. end
  24. for i=2:(n-1),
  25.      U(i,1) = feval(f,h*(i-1));
  26. end
  27. Vd = s1*ones(1,n);
  28. Vd(1) = 1;
  29. Vd(n) = 1;
  30. Va = -ones(1,n-1);
  31. Va(n-1) = 0;
  32. Vc = -ones(1,n-1);
  33. Vc(1) = 0;
  34. Vb(1) = feval(g1,k*0);
  35. Vb(n) = feval(g2,k*0);
  36. for j=2:m,
  37.   for i=2:(n-1),
  38.        Vb(i) = U(i-1,j-1) + U(i+1,j-1) + s2*U(i,j-1);
  39.   end
  40.      X = trisys(Va,Vd,Vc,Vb);
  41.      U(1:n,j) = X';
  42. end
  43.